(function() {

	var appModule = angular.module('tracecashModule');

	appModule.directive('actionList', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/actions/action-list.html',
			require : '^ngModel',
			controller : 'ActionListCtrl',
			scope : {
				sinister : '=ngModel',
				message : '=',
				config : '='
			}
		};
	});

	appModule.controller('ActionListCtrl', ['$scope', '$rootScope', '$routeParams', '$http', '$modal', 'configService', function($scope, $rootScope, $routeParams, $http, $modal, configService) {
		$scope.open = function(controllerName, templateName) {
			var modalInstance = $modal.open({
				templateUrl : 'html/actions/' + templateName,
				controller : controllerName,
				size : 'lg',
				resolve : {
					sinister : function() {
						return $scope.sinister;
					}
				}
			});
			modalInstance.result.then(function(message) {
				$scope.message = message;
				$scope.sinister = message.payload;
				$scope.config = configService.create($scope.sinister.state);
			}, function() {

			});
		};
	}]);

	appModule.directive('defaultRejectionReason', function() {
		return {
			restrict : 'AE',
			template : '<select class="form-control" ng-model="ngModel" ng-options="reason.title for reason in reasons"></select>',
			require : '^ngModel',
			controller : ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
				$http.get($rootScope.REST_PATH + '/rejection/list/default').success(function(data) {
					$scope.reasons = data;
				});
			}],
			scope : {
				ngModel : '='
			}
		};
	});

	appModule.controller('SinisterRejectionActionCtrl', ['$scope', '$rootScope', '$http', '$modalInstance', 'sinister', function($scope, $rootScope, $http, $modalInstance, sinister) {
		$scope.action = {};
		$scope.action.sinister = sinister;
		$scope.$watch('action.defaultReason.description', function(text) {
			$scope.action.reason = text;
		});
		$scope.save = function() {
			$http.post($rootScope.REST_PATH + '/rejection/sinister/' + sinister.id, $scope.action).success(function(data) {
				if (data.code == $rootScope.CODE_SUCCESS) {
					$modalInstance.close(data);
				} else {
					$scope.message = data;
				}
			});
		};
		$scope.cancel = function() {
			$modalInstance.dismiss('cancel');
		};
	}]);

	appModule.controller('SinisterReopenActionCtrl', ['$scope', '$rootScope', '$http', '$modalInstance', 'sinister', function($scope, $rootScope, $http, $modalInstance, sinister) {
		$scope.sinister = sinister;
		var sinisterYear = sinister.sinisterDate.substring(0, 4), currentYear = new Date().getFullYear();
		$scope.canChangeInitialReserve = sinisterYear < currentYear;

		$scope.save = function() {
			$http.get($rootScope.REST_PATH + '/reopen/sinister/' + sinister.id, {
				params : {
					newInitialReserve : $scope.newInitialReserve
				}
			}).success(function(message) {
				if (message.code == $rootScope.CODE_SUCCESS) {
					$modalInstance.close(message);
				} else {
					$scope.message = message;
				}
			});
		};
		$scope.cancel = function() {
			$modalInstance.dismiss('cancel');
		};
	}]);

	appModule.controller('SinisterLiquidationActionCtrl', ['$scope', '$rootScope', '$http', '$modalInstance', 'sinister', function($scope, $rootScope, $http, $modalInstance, sinister) {
		$scope.sinister = sinister;
		$scope.save = function() {
			$http.post($rootScope.REST_PATH + '/reservevariation/empty/' + sinister.id, {}).success(function(message) {
				if (message.code == $rootScope.CODE_SUCCESS) {
					$modalInstance.close(message);
				} else {
					$scope.message = message;
				}
			});
		};
		$scope.cancel = function() {
			$modalInstance.dismiss('cancel');
		};
	}]);

})();